<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.activityUserName"
            placeholder="参加公益活动的姓名"
            clearable
          ></el-input>
          <el-input
            v-model="searchModel.activityName"
            placeholder="参加的活动"
            clearable
          ></el-input>
          <el-button
            @click="getActivityList"
            type="primary"
            round
            icon="el-icon-search"
          >查询
          </el-button
          >
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            @click="openEditUI(null)"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="activityList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="activityUserName" label="参与人姓名" width="180">
        </el-table-column>
        <el-table-column prop="activityName" label="公益活动" width="180">
        </el-table-column>
        <el-table-column prop="activityUserPhone" label="参与人电话" width="180">
        </el-table-column>
        <el-table-column prop="activityAddress" label="地址"></el-table-column>
        <el-table-column prop="activityTime" label="参与开启时间"></el-table-column>
<!--        <el-table-column label="操作" width="180">-->
<!--          <template slot-scope="scope">-->
<!--            <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" size="mini"-->
<!--                       circle></el-button>-->
<!--            <el-button @click="deleteActivity(scope.row)" type="danger" icon="el-icon-position" size="mini"-->
<!--                       circle></el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 用户信息编辑对话框 -->
    <el-dialog
      @close="clearForm"
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="activityForm" ref="activityFormRef" :rules="rules">
        <el-form-item
          label="活动参与人"
          placeholder="请输入您的姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="activityForm.activityUserName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="activityForm.activityName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="activityForm.activityAddress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="activityForm.activityUserPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="参与时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="activityForm.activityTime"
            type="date"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveActivity">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import activityApi from "@/api/activity";

export default {
  data() {
    return {
      formLabelWidth: "130px",
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      activityForm: {
        activityUserName: '',
        activityName: '',
        activityAddress: '',
        activityUserPhone:'',
        activityTime: ''
      },
      activityList: [],
      rules: {
        activityName: [
          {required: true, message: "请输入用户名", trigger: "blur"},
          {
            min: 3,
            max: 50,
            message: '长度在 3 到 50 个字符',
            trigger: "blur",
          },
        ]
      },
    };
  },
  methods: {
    deleteActivity(activity) {
      this.$confirm(`您确定删除该公益活动 ${activity.activityName} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        activityApi.deleteActivityById(activity.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          });
          this.getActivityList();
        });

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    saveActivity() {
      // 触发表单验证
      this.$refs.activityFormRef.validate((valid) => {
        if (valid) {
          // 提交请求给后台
          activityApi.saveActivity(this.activityForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            });
            // 关闭对话框
            this.dialogFormVisible = false;
            // 刷新表格
            this.getActivityList();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });

    },
    clearForm() {
      this.activityForm = {};
      this.$refs.activityFormRef.clearValidate();
    },
    openEditUI(id) {
      if (id == null) {
        this.title = "新增公益活动信息";
      } else {
        this.title = "修改公益活动信息";
        // 根据id查询用户数据
        activityApi.getActivityById(id).then(response => {
          this.activityForm = response.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getActivityList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getActivityList();
    },
    getActivityList() {
      activityApi.getActivityList(this.searchModel).then((response) => {
        this.activityList = response.data.rows;
        this.total = response.data.total;
      });
    },
  },
  created() {
    this.getActivityList();
  },
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}
</style>
